/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ticketManager.ejb;

import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import ticketManager.model.ManagerUser;
import ticketManager.model.Problem;
import ticketManager.model.ProblemStates;
import ticketManager.model.Product;

/**
 *
 * @author luchenyuxx
 */
@Stateless
public class ProblemDAO {

    @PersistenceContext
    EntityManager em;

    public List<Problem> getNewProblems() {
        List<Problem> newProblems = em.createQuery("select p from Problem p where p.state =:n").setParameter("n", ProblemStates.NewlyCreated)
                .getResultList();
        return newProblems;
    }

    public List<Problem> getSolvedProblems() {
        List<Problem> solvedProblems = em.createQuery("select p from Problem p where p.state=:s").setParameter("s", ProblemStates.Solved)
                .getResultList();
        return solvedProblems;
    }

    public List<Problem> getTransferProblems() {
        List<Problem> transferProblems = em.createQuery("select p from Problem p where p.state=:t").setParameter("t", ProblemStates.TransferRequest)
                .getResultList();
        return transferProblems;
    }

    public List<Problem> getRejectedProblems() {
        List<Problem> rejectedProblems = em.createQuery("select p from Problem p where p.state=:r").setParameter("r", ProblemStates.Rejected)
                .getResultList();
        return rejectedProblems;
    }

    public List<Problem> getInWorkProblems() {
        List<Problem> inWorkProblems = em.createQuery("select p from Problem p where p.state=:inWork or p.state=:testing")
                .setParameter("inWork", ProblemStates.InWork).setParameter("testing", ProblemStates.Testing).getResultList();
        return inWorkProblems;
    }

    public List<Problem> getAllProblems() {
        List<Problem> allProblems = em.createQuery("select p from Problem p").getResultList();
        return allProblems;
    }

    public Problem getProblemById(Long id) {
        Problem p = (Problem) em.createQuery("select p from Problem p where p.id=:problemId")
                .setParameter("problemId", id).getSingleResult();
        return p;
    }

    public List<Problem> getProblemsByProcess(ManagerUser m) {
        List<Problem> problems = em.createQuery("select p from Problem p where p.solver=:m").setParameter("m", m).getResultList();
        return problems;
    }
    
    public List<Problem> getProblemsByProduct(Product p)
    {
        List<Problem> problems = em.createQuery("select p from Problem p where p.productinvolve=:product").setParameter("product", p).getResultList();
        return problems;
    }

    /**
     * written by LGY
     *
     * @param title
     * @param description
     * @param m
     * @param p
     * @return 
     */
    public Problem createProblem(String title, String description, ManagerUser m, Product p) {
        Problem newProblem = new Problem();
        newProblem.setTitle(title);
        newProblem.setDescription(description);
        newProblem.setCreator(m);
        newProblem.setState(ProblemStates.NewlyCreated);
        newProblem.setProductinvolve(p);
        newProblem.setDateOfCreation(new Date());
        em.persist(newProblem);
        return newProblem;
    }

    public Problem changeProblemState(Problem p, ProblemStates state) {

        
        p.setState(state);
        em.merge(p);
        return p;
    }
}
